package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.dto.ChangePasswordDTO;
import com.example.dto.LoginDTO;
import com.example.dto.RegisterDTO;
import com.example.dto.UpdateProfileDTO;
import com.example.service.UserService;
import com.example.vo.LoginVO;
import com.example.vo.TokenRefreshVO;
import com.example.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/api/auth")
@CrossOrigin
public class AuthController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    @com.example.common.OperationLog(
        module = "系统登录",
        operationType = "LOGIN",
        description = "用户登录",
        recordParams = false,
        recordResult = false
    )
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        LoginVO loginVO = userService.login(loginDTO);
        return Result.success(loginVO);
    }
    
    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<Void> register(@RequestBody RegisterDTO registerDTO) {
        userService.register(registerDTO);
        return Result.success();
    }
    
    /**
     * 获取当前用户信息
     */
    @GetMapping("/userinfo")
    @RequirePermission("user:profile:view")
    public Result<UserInfoVO> getUserInfo() {
        UserInfoVO userInfo = userService.getCurrentUserInfo();
        return Result.success(userInfo);
    }
    
    /**
     * 更新当前用户资料
     */
    @PutMapping("/profile")
    @RequirePermission("user:profile:edit")
    @com.example.common.OperationLog(
        module = "个人中心",
        operationType = "UPDATE",
        description = "修改个人信息",
        recordParams = true
    )
    public Result<Void> updateProfile(@RequestBody UpdateProfileDTO updateProfileDTO) {
        userService.updateCurrentUserProfile(updateProfileDTO);
        return Result.success();
    }
    
    /**
     * 修改当前用户密码
     */
    @PutMapping("/change-password")
    @RequirePermission("user:password:change")
    @com.example.common.OperationLog(
        module = "个人中心",
        operationType = "UPDATE",
        description = "修改密码",
        recordParams = false
    )
    public Result<Void> changePassword(@RequestBody ChangePasswordDTO changePasswordDTO) {
        userService.changeCurrentUserPassword(changePasswordDTO);
        return Result.success();
    }

    /**
     * 刷新token
     */
    @PostMapping("/refresh-token")
    public Result<TokenRefreshVO> refreshToken(@RequestBody Map<String, String> request) {
        String refreshToken = request.get("refreshToken");
        if (refreshToken == null || refreshToken.isEmpty()) {
            return Result.error("刷新token不能为空");
        }

        TokenRefreshVO tokenRefreshVO = userService.refreshToken(refreshToken);
        return Result.success(tokenRefreshVO);
    }
} 